package com.kh.first.controller;

import com.kh.first.pojo.ProductReportPojo;
import com.kh.first.pojo.ReportDetailPojo;
import com.kh.first.pojo.common.AjaxJson;
import com.kh.first.service.ProductReportService;
import com.kh.first.service.ReportDetailService;
import com.kh.first.utils.UserUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

@Controller
@RequestMapping("reportDetailController")
public class ReportDetailController {

    @Autowired
    ReportDetailService reportDetailService;

    @Autowired
    ProductReportService productReportService;

    @RequestMapping(params = "saveReportDetail")
    @ResponseBody
    public AjaxJson<String> saveReportDetail(ReportDetailPojo reportDetailPojo) {
//        reportId 最重要的
        AjaxJson<String> ajaxJson = new AjaxJson<>();
        Long reportId = reportDetailPojo.getReportId();
        ProductReportPojo productReportById = productReportService.getProductReportById(reportId);
        Integer step = reportDetailPojo.getStep();
        if (null == step) {
            ajaxJson.code = -1;
            ajaxJson.data = "步骤为空";
            return ajaxJson;
        }
        //创建人工号,填写人员工号
        String userPinYin = UserUtils.getUserPinYin();//登录工号
        String createWorkNo = productReportById.getCreateUserWorkNo();
//        String ioUser = productReportById.getIoUser();//填写人工号
//        if (createWorkNo.equals(userPinYin)) {
//            ajaxJson.code = -1;
//            ajaxJson.data = "自填自批，不合规矩吧";
//            return ajaxJson;
//        }
//        if (!ioUser.contains(userPinYin)) {
//            ajaxJson.code = -1;
//            ajaxJson.data = "系统错误,权限不足";
//            return ajaxJson;
//        }
        reportDetailService.saveDetail(reportDetailPojo);
//        //--------------
//        Set<ConstraintViolation<ReportDetailPojo>> validate = Validation.buildDefaultValidatorFactory().getValidator().validate(reportDetailPojo);
//        StringBuilder sb = new StringBuilder();
//        if (validate.isEmpty()) {
//            Iterator<ConstraintViolation<ReportDetailPojo>> iterator = validate.iterator();
//            while (iterator.hasNext()) {
//                ConstraintViolation<ReportDetailPojo> next = iterator.next();
//                sb.append(next.getMessage() + ",");
//            }
//            ajaxJson.code = -1;
//            ajaxJson.data = sb.toString();
//            return ajaxJson;
//        }
        return ajaxJson;
    }
}
